package org.kzwl.service.web.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.CuHeritageBO;
import org.kzwl.comm.pojo.bo.QueryAdminCuHeritageBO;
import org.kzwl.comm.pojo.bo.heritage.HeritageBasicBO;
import org.kzwl.comm.pojo.bo.heritage.QueryDetailsBO;
import org.kzwl.comm.pojo.bo.heritage.QueryLimitBO;
import org.kzwl.comm.pojo.dto.heritage.HeritageBasicDTO;
import org.kzwl.comm.pojo.dto.heritage.QueryDetailsDTO;
import org.kzwl.entity.web.CuHeritage;
import org.kzwl.mapper.web.CuHeritageMapper;
import org.kzwl.service.web.CuHeritageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 非遗文化保护 服务实现类
 * </p>
 *
 * @author 张国伟/郭家恒
 * @since 2020-12-21
 */
@Service
public class CuHeritageServiceImpl extends ServiceImpl<CuHeritageMapper, CuHeritage> implements CuHeritageService {

    @Autowired
    private CuHeritageMapper heritageMapper;

    @Override
    public ResponseData<Void> saveOne(CuHeritageBO cuHeritageBO) {
        CuHeritage cuHeritage = new CuHeritage();
        BeanUtils.copyProperties(cuHeritageBO, cuHeritage);
        this.baseMapper.insert(cuHeritage);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<IPage<CuHeritage>> queryPage(QueryAdminCuHeritageBO queryAdminCuHeritageBO) {
        QueryWrapper<CuHeritage> cuHeritageQueryWrapper = new QueryWrapper<>();
        cuHeritageQueryWrapper
                .lambda()
                .apply(!"".equals(queryAdminCuHeritageBO.getStartDate()), "date_format (cu_heritage_date,'%Y-%m-%d') >= date_format('" + queryAdminCuHeritageBO.getStartDate() + "','%Y-%m-%d')")
                .apply(!"".equals(queryAdminCuHeritageBO.getEndDate()), "date_format (cu_heritage_date,'%Y-%m-%d') <= date_format('" + queryAdminCuHeritageBO.getEndDate() + "','%Y-%m-%d')");
        Page<CuHeritage> heritagePage = this.baseMapper.selectPage(new Page<>(queryAdminCuHeritageBO.getCurrent(), queryAdminCuHeritageBO.getSize()), cuHeritageQueryWrapper);
        return new ResponseData<>(heritagePage);
    }

    @Override
    public ResponseData<CuHeritage> queryOne(Long Id) {
        CuHeritage cuHeritage = this.baseMapper.selectById(Id);
        if (ObjectUtil.isNull(cuHeritage)) {
            throw new BizException("查无此信息");
        }
        return new ResponseData<>(cuHeritage);
    }

    @Override
    public ResponseData<Void> updateOne(CuHeritageBO cuHeritageBO) {
        if (ObjectUtil.isNull(this.baseMapper.selectById(cuHeritageBO.getCuHeritageId()))) {
            throw new BizException("查无此信息");
        }
        CuHeritage cuHeritage = new CuHeritage();
        BeanUtils.copyProperties(cuHeritageBO, cuHeritage);
        this.baseMapper.updateById(cuHeritage);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<Void> deleteId(Long Id) {
        CuHeritage cuHeritage = this.baseMapper.selectById(Id);
        if (ObjectUtil.isNull(cuHeritage)) {
            throw new BizException("查无此信息");
        }
        this.baseMapper.deleteById(Id);
        return new ResponseData<>();
    }

    //region 非遗基础查询
    @Override
    public IPage<HeritageBasicDTO> queryBasic(HeritageBasicBO bo) {
        IPage<CuHeritage> page = new Page<>();
        page.setCurrent(bo.getCurrent());
        page.setSize(bo.getSize());
        QueryWrapper<CuHeritage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CuHeritage::getCuHeritageCategoryId, bo.getCuHeritageCategoryId())
        .eq(bo.getCuHeritageType()!=null,CuHeritage::getCuHeritageType,bo.getCuHeritageType());
        page = heritageMapper.selectPage(page, queryWrapper);
        IPage<HeritageBasicDTO> dto = new Page<>();
        BeanUtils.copyProperties(page, dto);
        List<HeritageBasicDTO> list = JSONObject.parseArray(JSONObject.toJSONString(page.getRecords()), HeritageBasicDTO.class);
        dto.setRecords(list);
        return dto;
    }
    //endregion

    //region 非遗文化详细查询
    @Override
    public QueryDetailsDTO queryDetails(QueryDetailsBO bo) {
        CuHeritage cuHeritage = heritageMapper.selectById(bo.getCuHeritageId());
        if (cuHeritage == null) {
            throw new BizException("未查询到相关信息");
        }
        QueryDetailsDTO dto = new QueryDetailsDTO();
        BeanUtils.copyProperties(cuHeritage, dto);
        QueryWrapper<CuHeritage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().gt(CuHeritage::getCuHeritageId, dto.getCuHeritageId()).last(" limit 1");
        CuHeritage next = heritageMapper.selectOne(queryWrapper);
        if (next != null) {
            QueryDetailsDTO.Heritage heritage = new QueryDetailsDTO.Heritage();
            heritage.setCuHeritageId(next.getCuHeritageId());
            heritage.setCuHeritageTitle(next.getCuHeritageTitle());
            dto.setNext(heritage);
        }


        queryWrapper.clear();
        queryWrapper.lambda().lt(CuHeritage::getCuHeritageId, dto.getCuHeritageId()).orderByDesc(CuHeritage::getCuHeritageId).last(" limit 1");
        CuHeritage previous = heritageMapper.selectOne(queryWrapper);
        if (previous != null) {
            QueryDetailsDTO.Heritage heritage2 = new QueryDetailsDTO.Heritage();
            heritage2.setCuHeritageId(previous.getCuHeritageId());
            heritage2.setCuHeritageTitle(previous.getCuHeritageTitle());
            dto.setPrevious(heritage2);
        }
        return dto;
    }
    //endregion

    //region 查询头条
    @Override
    public List<HeritageBasicDTO> queryLimit(QueryLimitBO bo) {
        QueryWrapper<CuHeritage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(CuHeritage::getCuHeritageDate);
        if (bo.getQueryType()==0){
            queryWrapper.last(" limit 2");
        }else{
            queryWrapper.last(" limit 3");
        }
        List<CuHeritage> cuHeritages = heritageMapper.selectList(queryWrapper);
        List<HeritageBasicDTO> list = JSONObject.parseArray(JSONObject.toJSONString(cuHeritages), HeritageBasicDTO.class);
        return list;
    }
    //endregion

    //region 非遗保护首页推荐
    @Override
    public List<HeritageBasicDTO> queryRecommend() {
        QueryWrapper<CuHeritage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CuHeritage::getCuHeritageRecommendedIs,0).last(" limit 4");
        List<CuHeritage> cuHeritages = heritageMapper.selectList(queryWrapper);
        List<HeritageBasicDTO> list = JSONObject.parseArray(JSONObject.toJSONString(cuHeritages), HeritageBasicDTO.class);
        return list;
    }
    //endregion
}
